package com.open.capacity.workflow3.controller;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.open.capacity.commons.Result;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.workflow3.entity.NwfDelegateRelation;
import com.open.capacity.workflow3.entity.NwfSchemeinfo;
import com.open.capacity.workflow3.service.NwfDelegateService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.workflow3.entity.NwfDelegateRule;
import com.open.capacity.workflow3.service.NwfDelegateRuleService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * @author
 * @email
 * @date 2023-11-02 21:21:15
 */
@RestController
@RequestMapping("nwfdelegates")
@Api(tags = "")
public class NwfDelegateController {

    @Autowired
    private NwfDelegateService delegateService;

    @GetMapping("/getPageList")
    public PageResult getPageList(@RequestParam Map<String, Object> param, String keyword) {
        SysUser userInfo = SysUserUtil.getLoginAppUser();

        PageResult pageResult = delegateService.getPageList(param, keyword, userInfo);
        return pageResult;
    }

    @GetMapping("/getRelationList")
    public Result getRelationList(String keyValue) {
        List<NwfDelegateRelation> relationList = delegateService.getRelationList(keyValue);
        return Result.succeed(relationList);
    }

    @PostMapping("/saveForm")
    public Result saveForm(String keyValue, String strEntity, String strSchemeInfo) {
        NwfDelegateRule entity = JSONObject.parseObject(strEntity, new TypeReference<NwfDelegateRule>() {
        });
        delegateService.save(keyValue, entity, strSchemeInfo.split(","));
        return Result.succeed("保存成功！");
    }

    @RequestMapping("/deleteForm")
    public Result deleteForm(String keyValue) {
        delegateService.delete(keyValue);
        return Result.succeed("删除成功！");
    }

    @RequestMapping("/updateState")
    public Result updateState(String keyValue, int state) {
        delegateService.updateState(keyValue, state);
        return Result.succeed(((state == 1) ? "启用" : "禁用") + "成功！");
    }

}
